﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using API.CRUD.EF.Models;

namespace API.CRUD.EF.Controllers
{
    public class MahasiswaController : ApiController
    {
        [HttpGet]
        public OutputModel Select()
        {
            OutputModel output = new OutputModel();

            try
            {
                AkademikEntities db = new AkademikEntities();
                var data = db.Mahasiswas.Select(p => p);

                output.status = "success";
                output.message = String.Empty;
                output.data = data;
            }
            catch (Exception ex)
            {
                output.status = "error";
                output.message = ex.Message;
                output.data = null;
            }

            return output;
        }

        [HttpGet]
        public OutputModel SelectById(int nim)
        {
            OutputModel output = new OutputModel();

            try
            {
                AkademikEntities db = new AkademikEntities();
                Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();

                output.status = "success";
                output.message = String.Empty;
                output.data = item;
            }
            catch (Exception ex)
            {
                output.status = "error";
                output.message = ex.Message;
                output.data = null;
            }

            return output;
        }

        [HttpPost]
        public OutputModel Save([FromBody]Mahasiswa mhs)
        {
            OutputModel output = new OutputModel();

            try
            {
                AkademikEntities db = new AkademikEntities();
                db.Mahasiswas.Add(mhs);
                db.SaveChanges();

                output.status = "success";
                output.message = "Data mahasiswa berhasil disimpan.";
                output.data = String.Empty;
            }
            catch (Exception ex)
            {
                output.status = "error";
                output.message = ex.Message;
                output.data = null;
            }

            return output;
        }

        [HttpPut]
        public OutputModel Update(int nim, [FromBody]Mahasiswa mhs)
        {
            OutputModel output = new OutputModel();

            try
            {
                AkademikEntities db = new AkademikEntities();
                Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();

                item.Nama = mhs.Nama;
                item.Alamat = mhs.Alamat;
                item.Umur = mhs.Umur;
                item.Jurusan = mhs.Jurusan;

                db.SaveChanges();

                output.status = "success";
                output.message = "Data mahasiswa dengan NIM " + nim + " berhasil diupdate.";
                output.data = String.Empty;
            }
            catch (Exception ex)
            {
                output.status = "error";
                output.message = ex.Message;
                output.data = null;
            }

            return output;
        }

        [HttpDelete]
        public OutputModel Remove(int nim)
        {
            OutputModel output = new OutputModel();

            try
            {
                AkademikEntities db = new AkademikEntities();
                Mahasiswa item = db.Mahasiswas.Where(p => p.NIM.Equals(nim)).Single<Mahasiswa>();
                db.Mahasiswas.Remove(item);
                db.SaveChanges();

                output.status = "success";
                output.message = "Data mahasiswa dengan NIM " + nim + " berhasil dihapus.";
                output.data = String.Empty;
            }
            catch (Exception ex)
            {
                output.status = "error";
                output.message = ex.Message;
                output.data = null;
            }

            return output;
        }
    }
}